package com.gyul.findmystock.ui.stock.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gyul.findmystock.R;
import com.gyul.findmystock.entity.QuoteEntity;
import com.gyul.findmystock.util.Utils;


public class StockChartView extends View {
	static final String TAG = "StockChartView";
	
	TextView [] textStocks = new TextView[6];
	TextView [] textDates = new TextView[5];
	
	static float [] stockPoint = new float [4];
	
	
	float chartHeightPadding;	
	float chartHeight;
	float layoutHeight;
	float chartHeightGap;
		
	final int heightGapCount = 10;
	
	int zoomValue;
	float convertFactor;
	
	int minData;
	int maxData;
	
	Paint paintStockBlue;
	Paint paintStockRed;
	Paint paintStockDoji;
	Paint paintStockRange;
	
		
	public StockChartView(Context context) {
		super(context);
		
		initializeChartView();
	}
	
	public StockChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initializeChartView();
	}
	
	@Override
	public void onSizeChanged( int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		Log.d(TAG, "onSizeChanged");
		
		layoutHeight = h;
		chartHeight = h;
		chartHeightPadding = chartHeight / 12;		
		chartHeight = chartHeight - ( chartHeightPadding * 2 );
		chartHeightGap = chartHeight / heightGapCount;
		
		Chart.chartWidth = w;
		Chart.chartWidthGap = Chart.chartWidth / Chart.drawStockSize;
		Chart.chartWidthGapBlank = (Chart.chartWidthGap / 10) + 1;
				
		Chart.initialized = true;				 
	}
	
	private void initializeChartView() {
		paintStockBlue = new Paint();
		paintStockBlue.setStyle(Paint.Style.FILL);
		paintStockBlue.setColor(Color.BLUE);
		
		paintStockRed = new Paint();
		paintStockRed.setStyle(Paint.Style.FILL);
		paintStockRed.setColor(Color.RED);
		
		paintStockDoji = new Paint();
		paintStockDoji.setStyle(Paint.Style.FILL);
		paintStockDoji.setColor(Color.BLACK);
		
		paintStockRange = new Paint();
		paintStockRange.setStyle(Paint.Style.STROKE);
		paintStockRange.setColor(Color.BLACK);
		paintStockRange.setStrokeWidth(2);		
	}
	
	public void initializeChartInfo(View rootView) {
		Log.d(TAG, "initializeChartInfo()");
		
		textStocks[0] = (TextView) rootView.findViewById(R.id.text_stock_value1);
    	textStocks[1] = (TextView) rootView.findViewById(R.id.text_stock_value2);
    	textStocks[2] = (TextView) rootView.findViewById(R.id.text_stock_value3);
    	textStocks[3] = (TextView) rootView.findViewById(R.id.text_stock_value4);
    	textStocks[4] = (TextView) rootView.findViewById(R.id.text_stock_value5);
    	textStocks[5] = (TextView) rootView.findViewById(R.id.text_stock_value6);
    	
    	textDates[0] = (TextView) rootView.findViewById(R.id.text_stock_date1);
    	textDates[1] = (TextView) rootView.findViewById(R.id.text_stock_date2);
    	textDates[2] = (TextView) rootView.findViewById(R.id.text_stock_date3);
    	textDates[3] = (TextView) rootView.findViewById(R.id.text_stock_date4);
    	textDates[4] = (TextView) rootView.findViewById(R.id.text_stock_date5);
		
//		Log.d(TAG, "QuoteList Size : " + quoteList.size() + " StartIndex : " + Chart.startIndex);
	}
	
	@Override
	public void onDraw(Canvas canvas) {		
		if ( Chart.quoteList == null || Chart.initialized == false ) {
			return;
		}
		
		setFactor();
		
		canvas.drawColor(Chart.chartBackgroundColor);
		
		drawStock(canvas);
		drawRange();
		
		if ( Chart.showInfo ) {
			Chart.drawHighlightLine(canvas, layoutHeight);
		}
	}
	
	private void drawStock(Canvas canvas) {
		QuoteEntity entity = null;
		float stockX = 0;
		float left, right;
		float rangeX = Chart.chartWidthGap/2;
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {
			entity = Chart.quoteList.get(i);
//			Log.d(TAG, entity.toString());
			
			setData(entity);
			
//			Log.d(TAG, "open : " + stockPoint[0] + " close : " + stockPoint[1] + " low : " + stockPoint[2] + " high : " + stockPoint[3]);
			
			canvas.drawLine(rangeX, stockPoint[2], rangeX, stockPoint[3], paintStockRange);
			
			left = stockX + Chart.chartWidthGapBlank;
			right = stockX + Chart.chartWidthGap - Chart.chartWidthGapBlank;
			
			if ( entity.open < entity.close ) {
				if ( (stockPoint[0] - stockPoint[1]) < 10 ) {
					canvas.drawRect(left, stockPoint[0] - 5.0f, right, stockPoint[0] + 5.0f, paintStockDoji);
				} else {
					canvas.drawRect(left, stockPoint[1], right, stockPoint[0], paintStockRed);
				}
			} else {
				if ( (stockPoint[1] - stockPoint[0]) < 10 ) {
					canvas.drawRect(left, stockPoint[1] - 5.0f, right, stockPoint[1] + 5.0f, paintStockDoji);
				} else {
					canvas.drawRect(left, stockPoint[0], right, stockPoint[1], paintStockBlue);
				}
			}		
			
			stockX += Chart.chartWidthGap;		
			rangeX += Chart.chartWidthGap;
		}
	}
	
	
	
	private void drawRange() {
		int gap = (maxData - minData) / 5;
		int data = maxData;
		
		if ( textStocks[0] == null ) {
			return;
		}
		
		for ( int i = 0; i < 6; i++ ) {
			textStocks[i].setText("" + data);
			data -= gap;
		}
		
		int splitGap = Chart.drawStockSize / 5;
		int dateIndex = Chart.startIndex + (splitGap/2);
		
		for ( int i = 0; i <5; i++ ) {
			textDates[i].setText(Utils.getDateFormat(Chart.quoteList.get(dateIndex).date));
			dateIndex += splitGap;			
		}
	}
	
	private void setFactor() {
		setMinMax();
		convertFactor = chartHeightGap / ((maxData - minData) * 0.1f ); 
	}
	
	private void setData(QuoteEntity quote) {
		stockPoint[0] = chartHeightPadding + ( (maxData - quote.open) * convertFactor);
		stockPoint[1] = chartHeightPadding + ( (maxData - quote.close) * convertFactor);
		stockPoint[2] = chartHeightPadding + ( (maxData - quote.low) * convertFactor);
		stockPoint[3] = chartHeightPadding + ( (maxData - quote.high) * convertFactor);
	}
	
	private void setMinMax() {
		minData = Integer.MAX_VALUE;
		maxData = Integer.MIN_VALUE;
		
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {
			minData = Math.min(minData, Chart.quoteList.get(i).low);
			maxData = Math.max(maxData, Chart.quoteList.get(i).high);
		}
	}
}
